home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / oleo-1_4.lha / oleo-1.4 / cmd.c < prev    next >
C/C++ Source or Header  |  1993-05-22  |  54KB  |  2,355 lines

  1. /*    Copyright (C) 1993 Free Software Foundation, Inc.
  2.  
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2, or (at your option)
  6. any later version.
  7.  
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11. GNU General Public License for more details.
  12.  
  13. You should have received a copy of the GNU General Public License
  14. along with this software; see the file COPYING.  If not, write to
  15. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  16.  
  17.  
  18.  
  19. #include <ctype.h>
  20. #include <errno.h>
  21. #define obstack_chunk_alloc ck_malloc
  22. #define obstack_chunk_free free
  23. #include "obstack.h"
  24. #include "global.h"
  25. #include "cmd.h"
  26. #include "io-term.h"
  27. #include "io-abstract.h"
  28. #include "io-generic.h"
  29. #include "io-utils.h"
  30. #include "io-edit.h"
  31. #include "stub.h"
  32. #include "ref.h"
  33.  
  34. #undef MIN
  35. #undef MAX
  36. #define MIN(A,B) ((A) < (B) ? (A) : (B))
  37. #define MAX(A,B) ((A) > (B) ? (A) : (B))
  38.  
  39. /* Bogus mapping from KEY->CODE to ranges.  This is how bound
  40.  * macros are represented.
  41.  * This is bogus because the ranges will not be adjusted in 
  42.  * the ways they should.  Variables should be used instead. 
  43.  */
  44. int n_bound_macros;
  45. struct rng *bound_macros;
  46. int bound_macro_vec;
  47.  
  48.  
  49.  
  50. /* Flow of control centers around a select loop.  These are the 
  51.  * fd's selected on.
  52.  */
  53. SELECT_TYPE read_fd_set;
  54. SELECT_TYPE exception_fd_set;
  55. SELECT_TYPE write_fd_set;
  56.  
  57. /* These are fd's returned by the last call to select.
  58.  */
  59. SELECT_TYPE read_pending_fd_set;
  60. SELECT_TYPE exception_pending_fd_set;
  61. SELECT_TYPE write_pending_fd_set;
  62.  
  63. /* Hooks for asynchronos i/o
  64.  */
  65.  
  66. struct select_hook file_read_hooks[SELECT_SET_SIZE] = {0};
  67. struct select_hook file_exception_hooks[SELECT_SET_SIZE] = {0};
  68. struct select_hook file_write_hooks[SELECT_SET_SIZE] = {0};
  69.  
  70.  
  71. /* The current stream from which commands are being read. */
  72. struct input_stream * the_input_stream = 0;
  73.  
  74.  
  75.  
  76.  
  77. #ifdef __STDC__
  78. static struct input_stream *
  79. default_input_stream (void)
  80. #else
  81. static struct input_stream *
  82. default_input_stream ()
  83. #endif
  84. {
  85.   if (!the_input_stream)
  86.     {
  87.       the_input_stream =
  88.     (struct input_stream *)ck_malloc (sizeof (struct input_stream));
  89.       the_input_stream->_rmac = 0;
  90.       the_input_stream->_func_arg = 0;
  91.       obstack_init (&the_input_stream->_macro_stack);
  92.       the_input_stream->_macro = 0;
  93.       the_input_stream->_macro_start = 0;
  94.       the_input_stream->_macro_size = 0;
  95.       the_input_stream->prev_stream = 0;
  96.       the_input_stream->_last_macro = 0;
  97.       the_input_stream->_pushed_back_char = -1;
  98.     }
  99.   return the_input_stream;
  100. }
  101.  
  102.  
  103. /* This constructs an input stream that reads from a macro but never
  104.  * from a keyboard.  EXECUTE_CMD uses this.
  105.  */
  106.  
  107.  
  108. #ifdef __STDC__
  109. static struct input_stream *
  110. macro_only_input_stream (struct rng * rng, char * first_line, int len,
  111.              struct command_frame * frame)
  112. #else
  113. static struct input_stream *
  114. macro_only_input_stream (rng, first_line, len, frame)
  115.      struct rng * rng;
  116.      char * first_line;
  117.      int len;
  118.      struct command_frame * frame;
  119. #endif
  120. {
  121.   struct input_stream * ret;
  122.   ret = (struct input_stream *)ck_malloc (sizeof (struct input_stream));
  123.   ret->_func_arg = 0;
  124.   obstack_init (&ret->_macro_stack);
  125.   ret->_rmac =
  126.     (struct macro *) obstack_alloc (&ret->_macro_stack, sizeof (struct macro));
  127.   ret->_rmac->mac_prev = 0;
  128.   ret->_rmac->mac_rng = *rng;
  129.   ret->_rmac->mac_row = rng->lr;
  130.   ret->_rmac->mac_col = rng->lc;
  131.   (void) obstack_grow (&ret->_macro_stack, first_line, len);
  132.   (void) obstack_grow (&ret->_macro_stack, "", 1);
  133.   ret->_rmac->mac_start = ret->_rmac->mac_exe
  134.     = (unsigned char *) obstack_finish (&ret->_macro_stack);
  135.   ret->_macro = 0;
  136.   ret->_macro_start = 0;
  137.   ret->_macro_size = 0;
  138.   ret->_last_macro = 0;
  139.   ret->prev_stream = frame->input;
  140.   {
  141.     struct input_stream * key = frame->input;
  142.     while (frame->input == key)
  143.       {
  144.     frame->input = ret;
  145.     frame = frame->prev;
  146.       }
  147.   }
  148.   ret->_pushed_back_char = -1;
  149.   return ret;
  150. }
  151.  
  152. #ifdef __STDC__
  153. void
  154. free_input_stream (struct input_stream * stream)
  155. #else
  156. void
  157. free_input_stream (stream)
  158.      struct input_stream * stream;
  159. #endif
  160. {
  161.   if (stream->_macro_start)
  162.     free (stream->_macro_start);
  163.   if (stream->_last_macro)
  164.     free (stream->_last_macro);
  165.   obstack_free (&stream->_macro_stack, 0);
  166.   free (stream);
  167. }
  168.  
  169. /* This gets rid of an input stream created by macro_only_input_stream.
  170.  * It fixes the INPUT fields of pending command frames.
  171.  */
  172.  
  173. #ifdef __STDC__
  174. void
  175. pop_input_stream (void)
  176. #else
  177. void
  178. pop_input_stream ()
  179. #endif
  180. {
  181.   if (the_cmd_frame->input->prev_stream)
  182.     {
  183.       struct command_frame * fr = the_cmd_frame;
  184.       struct input_stream * key = the_cmd_frame->input;
  185.       while (fr->input == key)
  186.     {
  187.       fr->input = key->prev_stream;
  188.       fr = fr->prev;
  189.     }
  190.       free_input_stream (key);
  191.       return;
  192.     }
  193. }
  194.  
  195.  
  196. /* Macros 
  197.  * These are the commands the user has to interact with macros.
  198.  */
  199.  
  200. #ifdef __STDC__
  201. void
  202. start_entering_macro (void)
  203. #else
  204. void
  205. start_entering_macro ()
  206. #endif
  207. {
  208.   if (making_macro)
  209.     {
  210.       io_error_msg ("Can't define two macros at once");
  211.       return;
  212.     }
  213.   making_macro_size = 20;
  214.   making_macro = making_macro_start = ck_malloc (5 + making_macro_size);
  215. }
  216.  
  217. #ifdef __STDC__
  218. void
  219. bound_macro (int num)
  220. #else
  221. void
  222. bound_macro (num)
  223.      int num;
  224. #endif
  225. {
  226.   struct macro *old;
  227.   CELL *cp;
  228.  
  229.   cp = find_cell (bound_macros[num].lr, bound_macros[num].lc);
  230.   if (!cp || GET_TYP (cp) != TYP_STR || cp->cell_str[0] == '\0')
  231.     return;
  232.   old = rmac;
  233.   rmac =
  234.     (struct macro *) obstack_alloc (¯o_stack, sizeof (struct macro));
  235.   rmac->mac_prev = old;
  236.   rmac->mac_rng = bound_macros[num];
  237.   rmac->mac_row = bound_macros[num].lr;
  238.   rmac->mac_col = bound_macros[num].lc;
  239.   obstack_grow (¯o_stack, cp->cell_str, 1 + strlen (cp->cell_str));
  240.   rmac->mac_start = rmac->mac_exe =
  241.     (unsigned char *) obstack_finish (¯o_stack);
  242. }
  243.  
  244. #ifdef __STDC__
  245. void
  246. run_string_as_macro (char * macro)
  247. #else
  248. void
  249. run_string_as_macro (macro)
  250.      char * macro;
  251. #endif
  252. {
  253.   struct rng rng;
  254.   /* This is going to continue the command loop
  255.    * as if some other command had been executed.
  256.    * That command shouldn't receive the same prefix
  257.    * arg that provided a repeat count.
  258.    */
  259.   how_many = 1;
  260.   set_line (&raw_prefix, "");
  261.   rng.lr = rng.hr = rng.lc = rng.hc = MIN_ROW;
  262.   /* Reset the keystate. */
  263.   cur_keymap = the_cmd_frame->top_keymap;
  264.   macro_only_input_stream (&rng, macro, strlen (macro), the_cmd_frame);
  265.   command_loop (1);
  266. }
  267.  
  268. #ifdef __STDC__
  269. void
  270. call_last_kbd_macro (int count)
  271. #else
  272. void
  273. call_last_kbd_macro (count)
  274.      int count;
  275. #endif
  276. {
  277.   if (!last_macro)
  278.     io_error_msg ("No keyboard macro entered.");
  279.   while (count-- > 0)
  280.     run_string_as_macro ((char *)last_macro);
  281. }
  282.  
  283. /* This command is automaticly inserted into the command stream
  284.  * when the end of a macro is reached.  
  285.  */
  286. #ifdef __STDC__
  287. void
  288. end_macro (void)
  289. #else
  290. void
  291. end_macro ()
  292. #endif
  293. {
  294.   CELL *cp;
  295.   struct macro *old;
  296.  
  297.   if (!rmac)
  298.     {
  299.       io_error_msg ("Not executing a macro!");
  300.       return;
  301.     }
  302.   if ((rmac->mac_row == rmac->mac_rng.hr)
  303.       && (rmac->mac_col == rmac->mac_rng.hc))
  304.     {
  305.       old = rmac->mac_prev;
  306.       obstack_free (¯o_stack, rmac);
  307.       rmac = old;
  308.     }
  309.   else
  310.     {
  311.       if (rmac->mac_row == rmac->mac_rng.hr)
  312.     {
  313.       rmac->mac_row = rmac->mac_rng.lr; 
  314.       rmac->mac_col++;
  315.     }
  316.       else
  317.     rmac->mac_row++;
  318.       
  319.       cp = find_cell (rmac->mac_row, rmac->mac_col);
  320.       
  321.       if (!cp || GET_TYP (cp) != TYP_STR || cp->cell_str[0] == '\0') 
  322.     {
  323.       old = rmac->mac_prev;
  324.       obstack_free (¯o_stack, rmac); 
  325.       rmac = old;
  326.     }
  327.       else
  328.     {
  329.       obstack_grow (¯o_stack, cp->cell_str, 1 + strlen (cp->cell_str));
  330.       rmac->mac_exe
  331.         = (unsigned char *) obstack_finish (¯o_stack);
  332.     }
  333.     }
  334. }
  335.  
  336.  
  337. /* This command is executed by the user to stop entering a macro.
  338.  */
  339. #ifdef __STDC__
  340. void
  341. stop_entering_macro (void)
  342. #else
  343. void
  344. stop_entering_macro ()
  345. #endif
  346. {
  347.   if (!making_macro)
  348.     {
  349.       if (rmac)
  350.     return;
  351.       io_error_msg ("Not defining a macro!");
  352.       return;
  353.     }
  354.  
  355.   making_macro[0] = '\0';
  356.   making_macro = 0;
  357.   if (last_macro)
  358.     ck_free (last_macro);
  359.   last_macro = making_macro_start;
  360.   making_macro_start = 0;
  361.   free (making_macro_start);
  362. }
  363.  
  364. #ifdef __STDC__
  365. void
  366. store_last_macro (struct rng * rng)
  367. #else
  368. void
  369. store_last_macro (rng)
  370.      struct rng * rng;
  371. #endif
  372. {
  373.   union vals z;
  374.   z.c_s = (char *)last_macro;
  375.   set_new_value (rng->lr, rng->lc, TYP_STR, &z);
  376. }
  377.  
  378.  
  379.  
  380. /* Scheduling 
  381.  *
  382.  * Scheduling is centered around the function real_get_chr
  383.  * which is allowed to block until an input event has occured.
  384.  * Before blocking, real_get_chr may evaluate cells and/or update 
  385.  * the display.
  386.  */
  387.  
  388.  
  389. /* Error messages are delivered to the user by invoking a command 
  390.  * that prompts with the error message, and waits for the user's next
  391.  * keypress.  This command shouldn't wait indefinitely.  After a short time,
  392.  * the error message should disappear.  This is accomplished by counting down
  393.  * a timer, then destorying the error message command frame and throwing an
  394.  * error.  The error is thrown directly rather than with io_error_msg in order
  395.  * to avoid circularity.
  396.  */
  397.  
  398. static void
  399. error_alarm ()
  400. {
  401.   if (the_cmd_frame->cmd && the_cmd_arg.timeout_seconds)
  402.     {
  403.       --the_cmd_arg.timeout_seconds;
  404.       if (!the_cmd_arg.timeout_seconds)
  405.     {
  406.       pop_unfinished_command ();
  407.       alarm_table[2].freq = 0;
  408.       longjmp (error_exception, 1);
  409.     }
  410.     }
  411.   else
  412.     alarm_table[2].freq = 0;
  413. }
  414.  
  415. struct alarm_entry alarm_table [3] = 
  416. {
  417.   {cell_alarm, 1, 0},
  418.   {error_alarm, 0, 0},
  419.   {0, 0}
  420. };
  421.  
  422. /* Function that get called whenever blocking times out. */
  423.  
  424. #ifdef __STDC__
  425. static void
  426. alarm_hooks (void)
  427. #else
  428. static void
  429. alarm_hooks ()
  430. #endif
  431. {
  432.   int x;
  433.   time_t now = time(0);
  434.   for (x = 0; alarm_table[x].fn; ++x)
  435.     if (alarm_table[x].freq
  436.     && ((now - alarm_table[x].last_time) >= alarm_table[x].freq))
  437.       {
  438.     alarm_table[x].last_time = now;
  439.     alarm_table[x].fn ();
  440.       }
  441. }
  442.  
  443.  
  444. #ifdef __STDC__
  445. static void
  446. select_hooks (void)
  447. #else
  448. static void
  449. select_hooks ()
  450. #endif
  451. {
  452.   int x;
  453.   for (x = 0; x < SELECT_SET_SIZE; ++x)
  454.     {
  455.       if (file_read_hooks[x].hook_fn && FD_ISSET (x, &read_pending_fd_set))
  456.     file_read_hooks[x].hook_fn (x);
  457.       FD_CLR (x, &read_pending_fd_set);
  458.       if (file_write_hooks[x].hook_fn && FD_ISSET (x, &write_pending_fd_set))
  459.     file_write_hooks[x].hook_fn (x);
  460.       FD_CLR (x, &write_pending_fd_set);
  461.       if (file_exception_hooks[x].hook_fn
  462.       && FD_ISSET (x, &exception_pending_fd_set))
  463.     file_exception_hooks[x].hook_fn (x);
  464.       FD_CLR (x, &exception_pending_fd_set);
  465.     }
  466. }
  467.  
  468. /* Block until we get a signal (unless system calls restart), 
  469.  * can do i/o or, until we timeout (timeout is specified in seconds,
  470.  * 0 means block indefinately).  (Front end to select)
  471.  */
  472. #ifdef __STDC__
  473. static void
  474. block_until_excitment (int timeout_seconds)
  475. #else
  476. static void
  477. block_until_excitment (timeout_seconds)
  478.      int timeout_seconds;
  479. #endif
  480. {
  481.   int ret;
  482.   struct timeval timeout;
  483.   struct timeval * time_p = 0;
  484.  
  485.   if (timeout_seconds)
  486.     {
  487.       timeout.tv_sec = timeout_seconds;
  488.       timeout.tv_usec = 0;
  489.       time_p = &timeout;
  490.     }
  491.   bcopy ((char *)&read_fd_set, (char *)&read_pending_fd_set,
  492.      sizeof (SELECT_TYPE)) ;
  493.   bcopy ((char *)&exception_fd_set,
  494.      (char *)&exception_pending_fd_set, sizeof (SELECT_TYPE));
  495.   bcopy ((char *)&write_fd_set,
  496.      (char *)&write_pending_fd_set, sizeof (SELECT_TYPE));
  497.   ret = select (SELECT_SET_SIZE,
  498.         &read_pending_fd_set, &write_pending_fd_set,
  499.         &exception_pending_fd_set, time_p);
  500.   if (ret < 0)
  501.     {
  502.       FD_ZERO (&read_pending_fd_set);
  503.       FD_ZERO (&write_pending_fd_set);
  504.       FD_ZERO (&exception_pending_fd_set);
  505.     }
  506. }
  507.  
  508. /* This is the main interact loop.  As quickly as possible
  509.  * it returns a character from the keyboard.  While waiting,
  510.  * it updates cells and the display.  If a macro is being defined,
  511.  * this function save characters in the macro.
  512.  */
  513.  
  514. #ifdef __STDC__
  515. int 
  516. real_get_chr (void)
  517. #else
  518. int 
  519. real_get_chr ()
  520. #endif
  521. {
  522.   int ch;            /* The char that will be returned. */
  523.  
  524.   /* Characters with the meta bit set are returned as
  525.    * two characters: ESC and a non-meta character.
  526.    * This buffers the non-meta character between calls.
  527.    * The value is 0 if no character is buffered, C+1 if
  528.    * C is buffered.
  529.    */
  530.   static int saved_char;
  531.  
  532.   /* A buffer of characters read in one burst from the kbd. */
  533.   static char ibuf[256];
  534.   static int i_in;        /* chars buffered */
  535.   static int i_cnt;        /* buffer position */
  536.  
  537.   alarm_hooks ();
  538.   if (saved_char)
  539.     {
  540.       ch = saved_char - 1;
  541.       saved_char = 0;
  542.       goto fini;
  543.     }
  544.  
  545.   if (i_cnt)
  546.     {
  547.       ch = ibuf[i_cnt++];
  548.       if (i_cnt == i_in)
  549.     i_cnt = i_in = 0;
  550.       goto fini;
  551.     }
  552.  
  553.   /* This loop until a character can be read. */
  554.   while (!io_input_avail ())
  555.     {
  556.       alarm_hooks ();
  557.       select_hooks ();
  558.       io_scan_for_input (0);
  559.       if (!io_input_avail ())
  560.     {
  561.       ++current_cycle;
  562.       if (auto_recalc && eval_next_cell ())
  563.         {
  564.           if (bkgrnd_recalc)
  565.         while (!io_input_avail () && eval_next_cell ())
  566.           io_scan_for_input (0);
  567.           else
  568.         while (eval_next_cell ())
  569.           ;
  570.           io_scan_for_input (0);
  571.           if (!io_input_avail ())
  572.         io_redisp ();
  573.           io_flush ();
  574.           io_scan_for_input (0);
  575.         }
  576.       else
  577.         {
  578.           int timeout = (alarm_active
  579.                  ? (alarm_seconds == 1
  580.                 ? 1
  581.                 : (alarm_seconds / 2))
  582.                  : 0);
  583.                  
  584.           --current_cycle;
  585.           io_redisp ();
  586.           io_flush ();
  587.           io_scan_for_input (0);
  588.           if (!io_input_avail ())
  589.         block_until_excitment (timeout);
  590.         }
  591.     }
  592.     }
  593.  
  594.   {
  595.     int ret;
  596.     ret = io_read_kbd (ibuf, sizeof (ibuf));
  597.     if (ret == 1)
  598.       {
  599.     ch = ibuf[0];
  600.     goto fini;
  601.       }
  602.     if (ret > 1)
  603.       {
  604.     i_cnt = 1;
  605.     i_in = ret;
  606.     ch = ibuf[0];
  607.     goto fini;
  608.       }
  609.     if (ret == 0 || errno != EINTR)
  610.       return EOF;
  611.   }
  612.  
  613. fini:
  614.  
  615.   if (ch & 0x80)
  616.     {
  617.       saved_char = 1 + (ch & 0x7f);
  618.       ch = CTRL ('[');
  619.     }
  620.  
  621.   if (making_macro)
  622.     {
  623.       /* This is stoopid and should be fixed.
  624.        * Macros (and other cell strings) should be 
  625.        * `struct line' and not c-strings.   -tl
  626.        */
  627.       if (ch == 0)
  628.     *making_macro++ = 0x80 | 'a';
  629.       else if (ch == '{')
  630.     *making_macro++ = 0x80 | 'b';
  631.       else
  632.     *making_macro++ = ch;
  633.       if (making_macro >= (making_macro_start + making_macro_size))
  634.     {
  635.       making_macro_start = ck_realloc (making_macro_start, 5
  636.                        + making_macro_size * 2);
  637.       making_macro = (making_macro_start + making_macro_size);
  638.       making_macro_size *= 2;
  639.     }
  640.     }
  641.   return ch;
  642. }
  643.  
  644.  
  645.  
  646.  
  647. /*****************************************************************
  648.  * 
  649.  * Command loops
  650.  *
  651.  * The basic cycle is that the user or a macro selects a function
  652.  * (while oleo updates the display and evaluates cells).
  653.  * A new command_frame is allocated in which to evaluate the selected 
  654.  * function.   Arguments to the function will be stored in this frame.
  655.  * The command loop interprets the FUNC_ARGS string of the selected function
  656.  * and builds an argument list.  If the FUNC_ARGS string specifies that
  657.  * the user must be prompted for an argument, an editting mode is entered
  658.  * and the command loop restarts.  The queue of command_frames form
  659.  * a stack of recursively invoked editting modes.
  660.  *
  661.  * When all of the arguments are ready, the command loop executes
  662.  * the function and discards its frame.
  663.  *
  664.  * In principle, any number of command_frames can be created and they
  665.  * could be evaluated in any order.  It is assumed in the code though that
  666.  * the frame the_cmd_frame->prev is the frame the user was in when
  667.  * the_cmd_frame was created (call it the `parent' frame).  Some arguments, 
  668.  * for example the prefix argument and the current row/col, are taken from the
  669.  * parent frame.  This is because those values may have changed in
  670.  * the_cmd_frame as the user editted arguments to the function being called.
  671.  */
  672.  
  673. /* The active command frame. This is the head of a queue which is used as a
  674.  * stack. 
  675.  */ 
  676. struct command_frame * the_cmd_frame = 0;
  677.  
  678. /* This is a list (next field) of frames that are currently running (their 
  679.  * commands are active on the c stack below the error_exception jump buffer).
  680.  */
  681. struct command_frame * running_frames = 0;
  682.  
  683.  
  684. /* This is called when the current frame has keymapped
  685.  * down to some function (stored in the_cmd_frame->_cur_cmd.
  686.  * This pushes a new frame in which the arguments to that
  687.  * command will be stored.
  688.  *
  689.  * This can also be called when the_cmd_frame is 0.  In that case,
  690.  * it will create a top-level frame.
  691.  *
  692.  */
  693.  
  694. #ifdef __STDC__
  695. void
  696. push_command_frame (struct rng * rng, char * first_line, int len)
  697. #else
  698. void
  699. push_command_frame (rng, first_line, len)
  700.      struct rng * rng;
  701.      struct line * first_line;
  702.      int len;
  703. #endif
  704. {
  705.   struct command_frame * new_cf =
  706.     (struct command_frame *)ck_malloc (sizeof (*new_cf));
  707.  
  708.   new_cf->next = new_cf;
  709.   new_cf->prev = new_cf;
  710.  
  711.   new_cf->input = (rng
  712.            ? macro_only_input_stream (rng, first_line, len, new_cf)
  713.            : default_input_stream ());
  714.  
  715.   new_cf->_setrow = NON_ROW;
  716.   new_cf->_setcol = NON_COL;
  717.   new_cf->_curow = MIN_ROW;
  718.   new_cf->_cucol = MIN_COL;
  719.   new_cf->_mkrow = NON_ROW;
  720.   new_cf->_mkcol = NON_COL;
  721.   new_cf->_input_active = 0;
  722.   new_cf->_window_after_input = -1;
  723.  
  724.   /* These may be reset later. */
  725.   new_cf->top_keymap = map_id ("main");
  726.   if (new_cf->top_keymap < 0)
  727.     new_cf->top_keymap = map_id ("universal");
  728.   new_cf->saved_cur_keymap = -1;
  729.   new_cf->_cur_keymap = map_id ("main");
  730.   new_cf->_how_many = 1;
  731.   new_cf->_cur_cmd = 0;
  732.   new_cf->_cur_vector = 0;
  733.   new_cf->_cur_chr = the_cmd_frame ? cur_chr : 0;
  734.  
  735.   init_line (&new_cf->_raw_prefix);
  736.   new_cf->_cmd_argc = 0;
  737.   new_cf->complex_to_user = 0;
  738.  
  739.   if (!the_cmd_frame)
  740.     {
  741.       /* This is a new top-level frame. */
  742.       the_cmd_frame = new_cf;
  743.       new_cf->cmd = 0;
  744.       new_cf->top_keymap =  map_id ("main");
  745.       if (new_cf->top_keymap < 0)
  746.     new_cf->top_keymap = map_id ("universal");
  747.     }
  748.   else if (cur_cmd)
  749.     {
  750.       new_cf->_cur_arg = 0;
  751.       new_cf->cmd = cur_cmd;
  752.       {
  753.     int argc = 0;
  754.     char ** prompt = new_cf->cmd->func_args;
  755.     while (prompt && *prompt)
  756.       {
  757.         new_cf->argv[argc].do_prompt = 0;
  758.         new_cf->argv[argc].is_set = 0;
  759.         new_cf->argv[argc].style = 0;
  760.         new_cf->argv[argc].arg_desc = *prompt;
  761.         new_cf->argv[argc].prompt = 0;
  762.         new_cf->argv[argc].expanded_prompt = 0;
  763.         new_cf->argv[argc].prompt_info = 0;
  764.         new_cf->argv[argc].info_line = 0;
  765.         init_line (&new_cf->argv[argc].text);
  766.         set_line (&new_cf->argv[argc].text, "");
  767.         new_cf->argv[argc].cursor = 0;
  768.         new_cf->argv[argc].overwrite = 0;
  769.         new_cf->argv[argc].inc_cmd = 0;
  770.         new_cf->argv[argc].timeout_seconds = 0;
  771.         bzero (&new_cf->argv[argc].val, sizeof (union command_arg_val));
  772.         ++argc;
  773.         ++prompt;
  774.       }
  775.     if (argc && new_cf->argv[0].arg_desc[0] == '+')
  776.       ++new_cf->argv[0].arg_desc;
  777.     new_cf->_cmd_argc = argc;
  778.     new_cf->_curow = curow;
  779.     new_cf->_cucol = cucol;
  780.     new_cf->_mkrow = mkrow;
  781.     new_cf->_mkcol = mkcol;
  782.     new_cf->_setrow = setrow;
  783.     new_cf->_setcol = setcol;
  784.     if (!rng)
  785.       new_cf->input = the_cmd_frame->input;
  786.       }
  787.     }
  788.  
  789.   new_cf->prev = the_cmd_frame;
  790.   new_cf->next = the_cmd_frame->next;
  791.   new_cf->prev->next = new_cf;
  792.   new_cf->next->prev = new_cf;
  793.   the_cmd_frame = new_cf;
  794. }
  795.  
  796. /* Remove a frame from the queue/stack. */
  797. #ifdef __STDC__
  798. void
  799. remove_cmd_frame (struct command_frame * frame)
  800. #else
  801. void
  802. remove_cmd_frame (frame)
  803.      struct command_frame * frame;
  804. #endif
  805. {
  806.   frame->next->prev = frame->prev;
  807.   frame->prev->next = frame->next;
  808.   if (the_cmd_frame == frame)
  809.     the_cmd_frame = frame->prev;
  810.   if (the_cmd_frame == frame)
  811.     {
  812.       the_cmd_frame = 0;
  813.       push_command_frame (0, 0, 0);
  814.     }
  815.   frame->next = frame->prev = 0;
  816. }
  817.  
  818.  
  819. /* This frees all of the memory allocated to FRAME (including
  820.  * the frame itself. 
  821.  */
  822. #ifdef __STDC__
  823. void
  824. free_cmd_frame (struct command_frame * frame)
  825. #else
  826. void
  827. free_cmd_frame (frame)
  828.      struct command_frame * frame;
  829. #endif
  830. {
  831.   if (frame->next)
  832.     remove_cmd_frame (frame);
  833.  
  834.   free_line (&frame->_raw_prefix);
  835.   if (frame->cmd)
  836.     {
  837.       int argc;
  838.       for (argc = 0; argc < frame->_cmd_argc; ++argc)
  839.     {
  840.       if (frame->argv[argc].is_set && frame->argv[argc].style->destroy)
  841.         frame->argv[argc].style->destroy (&frame->argv[argc]);
  842.       free_line (&frame->argv[argc].text);
  843.       if (frame->argv[argc].expanded_prompt &&
  844.           (frame->argv[argc].expanded_prompt != frame->argv[argc].prompt))
  845.         free (frame->argv[argc].expanded_prompt);
  846.     }
  847.     }
  848.   ck_free (frame);
  849. }
  850.  
  851.  
  852. /* Discard the current frame if it contains an unexecuted commnand. 
  853.  * This is used, for example, to handle break.
  854.  */
  855. #ifdef __STDC__
  856. void
  857. pop_unfinished_command (void)
  858. #else
  859. void
  860. pop_unfinished_command ()
  861. #endif
  862. {
  863.   if (the_cmd_frame->cmd)
  864.     {
  865.       int move_cursor = 0;
  866.       struct command_frame * frame = the_cmd_frame;
  867.       if (   frame->_curow != frame->prev->_curow
  868.       || frame->_cucol != frame->prev->_cucol)
  869.     {
  870.       io_hide_cell_cursor ();
  871.       move_cursor = 1;
  872.     }
  873.       remove_cmd_frame (frame);
  874.       if (move_cursor)
  875.     io_display_cell_cursor ();
  876.       free_cmd_frame (frame);
  877.     }
  878. }
  879.  
  880. /* This is called if an error has been signaled with io_error_msg.
  881.  * It discards any frames that the user has never interacted with
  882.  * and cancels all pending macros.  This is properly followed by 
  883.  * generating an error message for the user and longjmp to error_exception.
  884.  */
  885.  
  886. #ifdef __STDC__
  887. void
  888. recover_from_error (void)
  889. #else
  890. void
  891. recover_from_error ()
  892. #endif
  893. {
  894.   /* pop input streams until the bottom is reached. */
  895.   while (the_cmd_frame->input->prev_stream)
  896.     pop_input_stream ();
  897.  
  898.   /* cancel the current macros */
  899.   {
  900.     struct input_stream * stream = the_cmd_frame->input;
  901.     if (stream->_macro_start)
  902.       free (stream->_macro_start);
  903.     if (stream->_last_macro)
  904.       free (stream->_last_macro);
  905.     obstack_free (&stream->_macro_stack, 0);
  906.     obstack_init (&stream->_macro_stack);
  907.     stream->_rmac = 0;
  908.     stream->_func_arg = 0;
  909.     stream->_macro = stream->_macro_start = stream->_last_macro = 0;
  910.     stream->_macro_size = 0;
  911.     stream->_pushed_back_char = -1;
  912.   }
  913.  
  914.   /* pop command frames until an interactive one is reached. */
  915.   while (the_cmd_frame->prev != the_cmd_frame
  916.      && !the_cmd_frame->complex_to_user)
  917.     {
  918.       struct command_frame * fr = the_cmd_frame;
  919.       the_cmd_frame = the_cmd_frame->prev;
  920.       free_cmd_frame (fr);
  921.     }
  922.  
  923.   /* Discard any frames that were executing */
  924.   while (running_frames)
  925.     {
  926.       struct command_frame * f = running_frames;
  927.       running_frames = running_frames->next;
  928.       f->next = 0;
  929.       free_cmd_frame (f);
  930.     }
  931. }
  932.  
  933.  
  934.  
  935. /* When we begin editting a new argument, this function sets up the
  936.  * appropriate keymap, and then resets the state of the editting commands.
  937.  *
  938.  * The return value is 1 if the user must be prompted, 0 otherwise.
  939.  */
  940. #ifdef __STDC__
  941. static int
  942. get_argument (char * prompt, struct prompt_style * style)
  943. #else
  944. static int
  945. get_argument (prompt, style)
  946.      char * prompt;
  947.      struct prompt_style * style;
  948. #endif
  949. {
  950.   the_cmd_arg.style = style;
  951.   the_cmd_arg.prompt = prompt;
  952.   if (!the_cmd_arg.expanded_prompt)
  953.     the_cmd_arg.expanded_prompt = expand_prompt (prompt);
  954.   the_cmd_frame->top_keymap = map_id (the_cmd_arg.style->keymap);
  955.   the_cmd_arg.is_set = 0;
  956.   the_cmd_arg.do_prompt = 1;
  957.   if (the_cmd_frame->top_keymap < 0)
  958.     the_cmd_frame->top_keymap = map_id ("universal");
  959.   if (macro_func_arg)
  960.     {
  961.       set_line (&the_cmd_arg.text, macro_func_arg);
  962.       {
  963.     char * arg_ptr;
  964.     char * error;
  965.     arg_ptr = the_cmd_arg.text.buf;
  966.     error = the_cmd_arg.style->verify (&arg_ptr, &the_cmd_arg);
  967.     if (error)
  968.       {
  969.         macro_func_arg = 0;
  970.         io_error_msg ("%s", error);
  971.       }
  972.     else
  973.       {
  974.         the_cmd_arg.is_set = 1;
  975.         if (arg_ptr)
  976.           while (isspace (*arg_ptr))
  977.         ++arg_ptr;
  978.         if (arg_ptr && *arg_ptr)
  979.           macro_func_arg = arg_ptr;
  980.         else
  981.           macro_func_arg = 0;
  982.         return 0;
  983.       }
  984.       }
  985.     }
  986.   input_active = 1;
  987.   begin_edit ();
  988.  
  989.   /* Functions can come with macros that initialize arguments for the user. 
  990.    * As for the call to expand_prompt -- hehehehehe
  991.    */
  992.   if (the_cmd_frame->cmd->init_code && the_cmd_frame->cmd->init_code[cur_arg])
  993.     {
  994.       char * init_code = expand_prompt(the_cmd_frame->cmd->init_code[cur_arg]);
  995.       struct rng rng;
  996.       rng.lr = rng.hr = rng.lc = rng.hc = 1;
  997.       macro_only_input_stream (&rng, init_code, strlen (init_code),
  998.                    the_cmd_frame);
  999.       command_loop (1);
  1000.     }
  1001.  
  1002.   return 1;
  1003. }
  1004.  
  1005. #ifdef __STDC__
  1006. void
  1007. exit_minibuffer (void)
  1008. #else
  1009. void
  1010. exit_minibuffer ()
  1011. #endif
  1012. {
  1013.   if (check_editting_mode ())
  1014.     return;
  1015.   else
  1016.     {
  1017.       char * extra = the_cmd_arg.text.buf;
  1018.       char * error = the_cmd_arg.style->verify (&extra, &the_cmd_arg);
  1019.       if (error)
  1020.     {
  1021.       if (*error)
  1022.         io_error_msg ("%s", error);
  1023.     }
  1024.       else
  1025.     {
  1026.       if (extra)
  1027.         {
  1028.           while (isspace (*extra))
  1029.         ++extra;
  1030.           if (*extra)
  1031.         io_error_msg ("%s: extra characters in argument (%s)",
  1032.                   the_cmd_frame->cmd->func_name, extra);
  1033.         }
  1034.       the_cmd_arg.is_set = 1;
  1035.       input_active = 0;
  1036.       window_after_input = -1;
  1037.       topclear = 2;
  1038.     }
  1039.     }
  1040. }
  1041.  
  1042.  
  1043.  
  1044.  
  1045. #ifdef __STDC__
  1046. void
  1047. setn_arg_text (struct command_arg * arg, char * text, int len)
  1048. #else
  1049. void
  1050. setn_arg_text (arg, text, len)
  1051.      struct command_arg * arg;
  1052.      char * text;
  1053.      int len;
  1054. #endif
  1055. {
  1056.   setn_line (&arg->text, text, len);
  1057.   arg->cursor = len;
  1058. }
  1059.  
  1060. #ifdef __STDC__
  1061. void
  1062. init_arg_text (struct command_arg * arg, char * text)
  1063. #else
  1064. void
  1065. init_arg_text (arg, text)
  1066.      struct command_arg * arg;
  1067.      char * text;
  1068. #endif
  1069. {
  1070.   setn_arg_text (arg, text, strlen (text));
  1071. }
  1072.  
  1073. /* This apparently useless alias is here because
  1074.  * sometime in the future i want to handle defaults
  1075.  * differently.  
  1076.  */
  1077.  
  1078. #ifdef __STDC__
  1079. void
  1080. set_default_arg (struct command_arg * arg, char * text, int len)
  1081. #else
  1082. void
  1083. set_default_arg (arg, text, len)
  1084.      struct command_arg * arg;
  1085.      char * text;
  1086.      int len;
  1087. #endif
  1088. {
  1089.   setn_arg_text (arg, text, len);
  1090. }
  1091.  
  1092. /* This is the main loop of oleo.   It reads commands and their arguments, and
  1093.  * evaluates them.  It (via real_get_chr) udpates the display and performs
  1094.  * background recomputation.
  1095.  *
  1096.  * This function can also be used to evaluate a function without doing any
  1097.  * interaction.  This is done by pushing a macro_only command frame
  1098.  *  (see execute_command).
  1099.  */
  1100.  
  1101. #ifdef __STDC__
  1102. void
  1103. command_loop (int prefix)
  1104. #else
  1105. void
  1106. command_loop (prefix)
  1107.      int prefix;
  1108. #endif
  1109. {
  1110.  
  1111.   /* We might be re-entering after a longjmp caused by an error.
  1112.    * In that case, we use an alternate entry point:
  1113.    */
  1114.   if (the_cmd_frame->cmd)
  1115.     goto resume_getting_arguments;
  1116.  
  1117.   /* Commands (notably execute_command) just tweek the command_frame
  1118.    * state for some other command.  To accomplish this, there is an 
  1119.    * entry point that avoid reinitializing the command_frame.\
  1120.    */
  1121.   if (prefix)
  1122.     {
  1123.       prefix = 0;
  1124.       goto prefix_cmd_continuation;
  1125.     }
  1126.   
  1127.   while (1)
  1128.     {
  1129.       int ch;            /* The next character to be keymapped. */
  1130.  
  1131.     new_cycle:
  1132.  
  1133.       if (!the_cmd_frame)
  1134.     push_command_frame (0, 0, 0);
  1135.  
  1136.       /* Reset the prefix argument. */
  1137.       how_many = 1;
  1138.       set_line (&raw_prefix, "");
  1139.       io_update_status ();
  1140.  
  1141.       /* Reset the keystate. */
  1142.       cur_keymap = the_cmd_frame->top_keymap;
  1143.  
  1144.       /* If the input area holds a prompt from the last command,
  1145.        * erase it. 
  1146.        */
  1147.       io_clear_input_before ();
  1148.  
  1149.  
  1150.       /* Some commands are prefix commands: they effect the 
  1151.        * user's state without beginnging a new command cyle.
  1152.        * Those commands return here:
  1153.        */
  1154.  
  1155.     prefix_cmd_continuation:
  1156.  
  1157.       /* In this loop, we look for the next command to
  1158.        * execute.  This may involve reading from a macro, 
  1159.        * or the keyboard.  If there is time to kill, updates
  1160.        * and evalutations are done.
  1161.        *
  1162.        * This loop is exited by `goto got_command'.
  1163.        */
  1164.  
  1165.       while (1)
  1166.     {
  1167.       /* Get the next character.
  1168.        * However, if we are in a macro, and the next character
  1169.        * is '{', then the macro contains a function name
  1170.        * and keymapping is circumvented. 
  1171.        */
  1172.  
  1173.     get_next_char:
  1174.  
  1175.       if (pushed_back_char >= 0)
  1176.         {
  1177.           ch = pushed_back_char;
  1178.           pushed_back_char = -1;
  1179.         }
  1180.       else if (!rmac)
  1181.         {
  1182.           io_fix_input ();
  1183.           ch = real_get_chr ();
  1184.         }
  1185.       else
  1186.         {
  1187.           int len;
  1188.           unsigned char *ptr;
  1189.           
  1190.         tryagain:
  1191.           alarm_hooks ();
  1192.           ch = *(rmac->mac_exe++);
  1193.           switch (ch)
  1194.         {
  1195.         case '\0':
  1196.           cur_vector = 0;
  1197.           cur_cmd = end_macro_cmd;
  1198.           cur_chr = 0;
  1199.           goto got_command;
  1200.           
  1201.         case 0x80 | 'a':
  1202.           ch = '\0';
  1203.           break;
  1204.           
  1205.         case 0x80 | 'b':
  1206.           ch = '{';
  1207.           break;
  1208.         case 0x80 | 'c':
  1209.           ch = '}';
  1210.           break;
  1211.           
  1212.         case '{':
  1213.           for (ptr = rmac->mac_exe;
  1214.                *ptr && *ptr != ' ' && *ptr != '}';
  1215.                ptr++);
  1216.           len = ptr - rmac->mac_exe;
  1217.           for (cur_vector = 0;
  1218.                cur_vector < num_funcs;
  1219.                cur_vector++)
  1220.             for (cur_cmd =
  1221.              &the_funcs[cur_vector][0];
  1222.              cur_cmd->func_name;
  1223.              cur_cmd++)
  1224.               if (!strincmp ((char *) (rmac->mac_exe),
  1225.                      cur_cmd->func_name, len)
  1226.               && cur_cmd->func_name[len] == '\0')
  1227.             {
  1228.               cur_chr = '\0';
  1229.               goto out;
  1230.             }
  1231.           io_error_msg ("Ignoring unknown function '%.*s' in macro",
  1232.                 len, rmac->mac_exe);
  1233.           while (*ptr != '\0' && *ptr != '}')
  1234.             ptr++;
  1235.           if (*ptr == '}')
  1236.             ptr++;
  1237.           rmac->mac_exe = ptr;
  1238.           goto tryagain;
  1239.           
  1240.         out:
  1241.           if (*ptr == ' ')
  1242.             {
  1243.               /* ... add argument support here ... */
  1244.               if (!cur_cmd->func_args)
  1245.             {
  1246.               io_error_msg ("Ignoring extra operand to %s",
  1247.                     cur_cmd->func_name);
  1248.               while (*ptr && *ptr != '}')
  1249.                 ptr++;
  1250.               if (*ptr == '}')
  1251.                 ptr++;
  1252.             }
  1253.               else if (cur_cmd->func_args[0][0] == '+')
  1254.             {
  1255.               unsigned char * start = ptr;
  1256.               how_many = astol ((char **) (&ptr));
  1257.               setn_line (&raw_prefix, (char *)start,
  1258.                      ptr - start);
  1259.               if (*ptr == '}')
  1260.                 ptr++;
  1261.             }
  1262.               else
  1263.             {
  1264.               while (isspace(*ptr))
  1265.                 ++ptr;
  1266.               macro_func_arg = (char *) ptr;
  1267.               while (*ptr && *ptr != '}')
  1268.                 {
  1269.                   switch (*ptr)
  1270.                 {
  1271.                 case 0x80 | 'b':
  1272.                   *ptr = '{';
  1273.                   break;
  1274.                 case 0x80 | 'c':
  1275.                   *ptr = '}';
  1276.                   break;
  1277.                 }
  1278.                   ptr++;
  1279.                 }
  1280.               if (*ptr == '}')
  1281.                 *ptr++ = '\0';
  1282.             }
  1283.               rmac->mac_exe = ptr;
  1284.             }
  1285.           else
  1286.             rmac->mac_exe += len + 1;
  1287.           goto got_command;
  1288.         }
  1289.         }
  1290.  
  1291.       /* When control comes here, adjust the keystate according 
  1292.        * to the cur_keymap and `ch';
  1293.        */
  1294.     have_character:
  1295.  
  1296.       /* This is how keymaps are searched for a binding. */ 
  1297.       while (1)
  1298.         {
  1299.           struct key * key;
  1300.           key = &(the_maps[cur_keymap]->keys[ch]);
  1301.           if (key->vector < 0)
  1302.         {
  1303.           if (key->code >= 0)
  1304.             {
  1305.               cur_keymap = key->code;
  1306.               goto get_next_char;
  1307.             }
  1308.           else if (the_maps[cur_keymap]->map_next)
  1309.             cur_keymap =
  1310.               the_maps[cur_keymap]->map_next->id;
  1311.           else
  1312.             {
  1313.               cur_vector = 0;
  1314.               cur_cmd = 0;
  1315.               cur_chr = ch;
  1316.               goto got_command;
  1317.             }
  1318.         }
  1319.           else
  1320.         {
  1321.           cur_vector = key->vector;
  1322.           cur_cmd =
  1323.             &(the_funcs[key->vector][key->code]);
  1324.           cur_chr = ch;
  1325.           goto got_command;
  1326.         }
  1327.         }
  1328.     }
  1329.  
  1330.  
  1331.       /* Now the next command to begin has been read from a macro
  1332.        * or the keyboard.
  1333.        */
  1334.     got_command:
  1335.  
  1336.       /* If there is anything left in the input area (e.g. old error message)
  1337.        * get rid of it (but only if we're not executing a macro).
  1338.        */
  1339.       if (!rmac)
  1340.     io_clear_input_after ();
  1341.  
  1342.       /* There are some commands that are implemented right here. */
  1343.       if (cur_cmd == break_cmd)
  1344.     {
  1345.       io_bell ();
  1346.       set_info (0);
  1347.       if (input_active)
  1348.         pop_unfinished_command ();      /* Abort a complex command.*/
  1349.       goto new_cycle;
  1350.     }
  1351.  
  1352.       /* The binding of all keys associated with the prefix arg. */
  1353.       if (cur_cmd == universal_arg_cmd)
  1354.       {
  1355.       char ch = cur_chr;
  1356.       int prefix_map = map_id ("prefix");
  1357.       /* Make sure the prefix-arg keymap is in place. */
  1358.       if (cur_keymap != prefix_map)
  1359.         {
  1360.           the_cmd_frame->saved_cur_keymap = the_cmd_frame->top_keymap;
  1361.           cur_keymap = prefix_map;
  1362.         }
  1363.       /* Store the last character typed in the raw-prefix.*/
  1364.       catn_line (&raw_prefix, &ch, 1);
  1365.       /* Recompute the numeric value of the prefix. */
  1366.       {
  1367.         int x = 0;
  1368.         int presumed_digits = 0;
  1369.         int sign = 1;
  1370.  
  1371.         how_many = 1;
  1372.         while (raw_prefix.buf[x])
  1373.           {
  1374.         if (isdigit (raw_prefix.buf[x]))
  1375.           {
  1376.             if (presumed_digits)
  1377.               how_many = how_many * 10 + (raw_prefix.buf[x] - '0');
  1378.             else
  1379.               {
  1380.             presumed_digits = 1;
  1381.             how_many = raw_prefix.buf[x] - '0';
  1382.               }
  1383.           }
  1384.         else if (raw_prefix.buf[x] == '-')
  1385.           sign *= -1;
  1386.         else
  1387.           {
  1388.             if (presumed_digits)
  1389.               {
  1390.             presumed_digits = 0;
  1391.             how_many = 1;
  1392.               }
  1393.             how_many *= 4;
  1394.           }
  1395.         ++x;
  1396.           }
  1397.         how_many *= sign;
  1398.         io_update_status ();
  1399.         goto prefix_cmd_continuation; 
  1400.       }
  1401.     }
  1402.  
  1403.       /* Make sure we really mapped to a command. */
  1404.       if (!cur_cmd || !cur_cmd->func_func)
  1405.     {
  1406.       /* If a character is unmapped in the prefix map,
  1407.        * retry mapping in the last-used normal keymap.
  1408.        */
  1409.       if (the_cmd_frame->saved_cur_keymap >= 0)
  1410.         {
  1411.           cur_keymap = the_cmd_frame->saved_cur_keymap;
  1412.           the_cmd_frame->saved_cur_keymap = -1;
  1413.           goto have_character;
  1414.         }
  1415.       /* Otherwise, signal an error and start from the top keymap. */
  1416.       io_bell ();
  1417.       goto new_cycle;
  1418.     }
  1419.       
  1420.  
  1421.  
  1422.       /* The next step is to gather the arguments with which to call
  1423.        * the function interactively.
  1424.        */
  1425.       /* Whever a new command is encountered, we begin by creating a 
  1426.        * frame in which to store it's arguments.
  1427.        * This initializes the new frame on the basis of cur_cmd in 
  1428.        * the_cmd_frame.  
  1429.        */
  1430.       push_command_frame (0, 0, 0);
  1431.  
  1432.       /* After some other command finishes from underneath a complex command,
  1433.        * flow returns here. 
  1434.        */
  1435.       
  1436.     resume_getting_arguments:
  1437.  
  1438.       while (cur_arg < cmd_argc)
  1439.     {
  1440.       if (the_cmd_arg.is_set)
  1441.         goto next_arg;
  1442.       else if (the_cmd_arg.prompt)
  1443.         {
  1444.           begin_edit ();
  1445.           goto new_cycle;
  1446.         }
  1447.       else
  1448.         {
  1449.           /* If we're just starting on this argument, then parse the
  1450.            * FUNC_ARGS string. To continue this loop, use `goto next_arg;'.
  1451.            *
  1452.            * If user interaction is required, the appropriate keymap,
  1453.            * editting area, etc. is set up, and the command loop resumes
  1454.            * (`goto new_cycle').
  1455.            */
  1456.           char * prompt = the_cmd_arg.arg_desc;
  1457.           
  1458.           switch (*prompt)
  1459.         {
  1460.         case 'c':
  1461.           {
  1462.             ++prompt;
  1463.             if (*prompt == '#')
  1464.               {
  1465.             ++prompt;
  1466.             the_cmd_arg.val.integer = *prompt;
  1467.             the_cmd_arg.is_set = 1;
  1468.             the_cmd_arg.do_prompt = 0;
  1469.             the_cmd_arg.style = &int_constant_style;
  1470.             {
  1471.               char c[2];
  1472.               c[0] = cur_chr;
  1473.               c[1] = '\0';
  1474.               init_arg_text (&the_cmd_arg, c);
  1475.             }
  1476.             goto next_arg;
  1477.               }
  1478.             else if (*prompt == '\'')
  1479.               {
  1480.             the_cmd_arg.timeout_seconds = 30;
  1481.             alarm_table[1].freq = 1;
  1482.             ++prompt;
  1483.               }
  1484.             if (get_argument (prompt, &char_style))
  1485.               goto new_cycle;
  1486.             goto next_arg;
  1487.           }
  1488.         case 'C':
  1489.           {
  1490.             ++prompt;
  1491.             if (get_argument (prompt, &command_style))
  1492.               goto new_cycle;
  1493.             goto next_arg;
  1494.           }
  1495.         case 'd':
  1496.           {
  1497.             ++prompt;
  1498.             if (get_argument (prompt, &double_style))
  1499.               goto new_cycle;
  1500.             goto next_arg;
  1501.           }
  1502.         case 'f':
  1503.           {
  1504.             char type;
  1505.             struct prompt_style * style;
  1506.             ++prompt;
  1507.             type = *prompt;
  1508.             ++prompt;
  1509.             switch (type)
  1510.               {
  1511.               case 'r':
  1512.             style = &read_file_style;
  1513.             break;
  1514.               case 'w':
  1515.             style = &write_file_style;
  1516.             break;
  1517.               case 'n':
  1518.             style = &file_name_style;
  1519.             break;
  1520.               default:
  1521.             style = 0; /* shutup gcc -ansi -pendantic -Wall! */
  1522.             io_error_msg ("func_args bug for %s",
  1523.                       the_cmd_frame->cmd->func_name);
  1524.               }
  1525.             if (get_argument (prompt, style))
  1526.               goto new_cycle;
  1527.             goto next_arg;
  1528.           }
  1529.         case 'F':
  1530.           {
  1531.             ++prompt;
  1532.             if (get_argument (prompt, &format_style))
  1533.               goto new_cycle;
  1534.             goto next_arg;
  1535.           }
  1536.         case 'k':
  1537.           {
  1538.             ++prompt;
  1539.             the_cmd_arg.val.key.cmd.vector = -1;
  1540.             the_cmd_arg.val.key.cmd.code
  1541.               = the_cmd_frame->prev->top_keymap;
  1542.             the_cmd_arg.val.key.keys = &the_cmd_arg.text;
  1543.             if (get_argument (prompt, &keyseq_style))
  1544.               goto new_cycle;
  1545.             goto next_arg;
  1546.           }
  1547.         case 'K':
  1548.           {
  1549.             ++prompt;
  1550.             if (get_argument (prompt, &keymap_style))
  1551.               goto new_cycle;
  1552.             goto next_arg;
  1553.           }
  1554.         case 'l':
  1555.           {
  1556.             the_cmd_arg.val.integer = cur_chr;
  1557.             the_cmd_arg.is_set = 1;
  1558.             the_cmd_arg.do_prompt = 0;
  1559.             the_cmd_arg.style = &int_constant_style;
  1560.             {
  1561.               char c[2];
  1562.               c[0] = cur_chr;
  1563.               c[1] = '\0';
  1564.               init_arg_text (&the_cmd_arg, c);
  1565.             }
  1566.             goto next_arg;
  1567.           }
  1568.         case 'm':
  1569.           {
  1570.             int want_keyseq = 0;
  1571.             ++prompt;
  1572.             want_keyseq = (*prompt == '\'');
  1573.             if (want_keyseq)
  1574.               {
  1575.             char * map;
  1576.             ++prompt;
  1577.             map = expand_prompt (prompt);
  1578.             the_cmd_arg.val.key.cmd.vector = -1;
  1579.             the_cmd_arg.val.key.cmd.code = map_id (map);
  1580.             the_cmd_arg.val.key.keys = &the_cmd_arg.text;
  1581.               }
  1582.             else
  1583.               {
  1584.             if (mode_style.keymap)
  1585.               ck_free (mode_style.keymap);
  1586.             mode_style.keymap = expand_prompt (prompt);
  1587.               }
  1588.             if (get_argument (prompt, (want_keyseq
  1589.                            ? &keyseq_style
  1590.                            : &mode_style)))
  1591.               goto new_cycle;
  1592.             goto next_arg;
  1593.           }
  1594.         case 'M':
  1595.           if (modified)
  1596.             {
  1597.               ++prompt;
  1598.               if (get_argument (prompt, &yes_style))
  1599.             goto new_cycle;
  1600.               goto next_arg;
  1601.             }
  1602.           else
  1603.             {
  1604.               the_cmd_arg.is_set = 1;
  1605.               the_cmd_arg.do_prompt = 1;
  1606.               the_cmd_arg.style = &yes_style;
  1607.               init_arg_text (&the_cmd_arg, "yes");
  1608.               goto next_arg;
  1609.             }
  1610.         case 'p':
  1611.           {
  1612.             ++prompt;
  1613.             switch (*prompt)
  1614.               {
  1615.               default:
  1616.             the_cmd_arg.val.integer
  1617.               = the_cmd_frame->prev->_how_many;
  1618.             the_cmd_arg.is_set = 1;
  1619.             the_cmd_arg.do_prompt = 0;
  1620.             the_cmd_arg.style = &int_constant_style;
  1621.             init_arg_text
  1622.               (&the_cmd_arg,
  1623.                long_to_str ((long)the_cmd_arg.val.integer)); 
  1624.             break;
  1625.  
  1626.               case '?':    /* Command wants to know if prefix provided */
  1627.             the_cmd_arg.val.integer =
  1628.               (the_cmd_frame->prev->_raw_prefix.alloc
  1629.                && the_cmd_frame->prev->_raw_prefix.buf[0]);
  1630.             the_cmd_arg.is_set = 1;
  1631.             the_cmd_arg.do_prompt = 0;
  1632.             the_cmd_arg.style = &int_constant_style;
  1633.             init_arg_text (&the_cmd_arg,
  1634.                        the_cmd_arg.val.integer ? "1" : "0");
  1635.             break;
  1636.               }
  1637.             goto next_arg;
  1638.           }
  1639.         case 'N':
  1640.         case 'n':
  1641.           {
  1642.             long low = 0;
  1643.             long high = -1;
  1644.             char type = *prompt;
  1645.             ++prompt;
  1646.             if (*prompt == '[')
  1647.               {
  1648.             ++prompt;
  1649.             low = astol (&prompt);
  1650.             while (isspace (*prompt))  ++prompt;
  1651.             if (*prompt == ',') ++prompt;
  1652.             high = astol (&prompt);
  1653.             while (isspace (*prompt))  ++prompt;
  1654.             if (*prompt == ']') ++prompt;
  1655.               }
  1656.             if (   (type == 'N')
  1657.             && the_cmd_frame->prev->_raw_prefix.alloc
  1658.             && the_cmd_frame->prev->_raw_prefix.buf[0])
  1659.               {
  1660.             the_cmd_arg.val.integer
  1661.               = the_cmd_frame->prev->_how_many;
  1662.             the_cmd_arg.is_set = 1;
  1663.             the_cmd_arg.do_prompt = 1;
  1664.             the_cmd_arg.style = &number_style;
  1665.             if (   (low >= high)
  1666.                 && (   (low > the_cmd_arg.val.integer)
  1667.                 || (high < the_cmd_arg.val.integer)))
  1668.               io_error_msg
  1669.                 ("Out of range %d (should be in [%d-%d]).");
  1670.             else
  1671.               init_arg_text
  1672.                 (&the_cmd_arg,
  1673.                  long_to_str ((long)the_cmd_arg.val.integer));
  1674.               }
  1675.             else
  1676.               {
  1677.             if (get_argument (prompt, &number_style))
  1678.               goto new_cycle;
  1679.               }
  1680.             goto next_arg;
  1681.           }
  1682.         case 'r':
  1683.         case 'R':
  1684.           {
  1685.             if (*prompt != 'R' && mkrow != NON_ROW)
  1686.               {
  1687.             the_cmd_arg.val.range.lr = MIN(mkrow, curow);
  1688.             the_cmd_arg.val.range.hr = MAX(mkrow, curow);
  1689.             the_cmd_arg.val.range.lc = MIN(mkcol, cucol);
  1690.             the_cmd_arg.val.range.hc = MAX(mkcol, cucol);
  1691.             the_cmd_arg.is_set = 1;
  1692.             the_cmd_arg.do_prompt = 1;
  1693.             the_cmd_arg.style = &range_style;
  1694.             mkrow = NON_ROW;
  1695.             mkcol = NON_COL;
  1696.             init_arg_text (&the_cmd_arg,
  1697.                        range_name (&the_cmd_arg.val.range)); 
  1698.             goto next_arg;
  1699.               }
  1700.             else
  1701.               {
  1702.             ++prompt;
  1703.             if (get_argument (prompt, &range_style))
  1704.               goto new_cycle;
  1705.             goto next_arg;
  1706.               }
  1707.           }
  1708.         case 's':
  1709.           {
  1710.             {
  1711.               ++prompt;
  1712.               if (get_argument (prompt, &string_style))
  1713.             goto new_cycle;
  1714.               goto next_arg;
  1715.             }
  1716.           }
  1717.         case 'S':
  1718.           {
  1719.             {
  1720.               ++prompt;
  1721.               if (*prompt == '\'')
  1722.             ++prompt;
  1723.               if (get_argument (prompt, &symbol_style))
  1724.             goto new_cycle;
  1725.               goto next_arg;
  1726.             }
  1727.           }
  1728.         case 'V':
  1729.           {
  1730.             ++prompt;
  1731.             the_cmd_arg.inc_cmd = io_shift_cell_cursor;
  1732.             if (get_argument (prompt, &inc_cmd_style))
  1733.               goto new_cycle;
  1734.             goto next_arg;
  1735.           }
  1736.         case 'w':
  1737.           {
  1738.             {
  1739.               ++prompt;
  1740.               if (*prompt == '\'')
  1741.             ++prompt;
  1742.               if (get_argument (prompt, &word_style))
  1743.             goto new_cycle;
  1744.               goto next_arg;
  1745.             }
  1746.           }
  1747.         case '#':
  1748.           {
  1749.             ++prompt;
  1750.             
  1751.             init_arg_text (&the_cmd_arg, prompt);
  1752.             the_cmd_arg.val.integer = astol(&prompt);
  1753.             the_cmd_arg.is_set = 1;
  1754.             the_cmd_arg.do_prompt = 0;
  1755.             the_cmd_arg.style = &int_constant_style;
  1756.             goto next_arg;
  1757.           }
  1758.         case '=':
  1759.           {
  1760.             ++prompt;
  1761.             the_cmd_arg.expanded_prompt = expand_prompt(prompt);
  1762.             init_arg_text (&the_cmd_arg, the_cmd_arg.expanded_prompt);
  1763.             the_cmd_arg.val.string = the_cmd_arg.expanded_prompt;
  1764.             the_cmd_arg.is_set = 1;
  1765.             the_cmd_arg.do_prompt = 0;
  1766.             the_cmd_arg.style = &string_style;
  1767.             goto next_arg;
  1768.           }
  1769.         case '.':
  1770.           {
  1771.             ++prompt;
  1772.             the_cmd_arg.val.range.lr = curow;
  1773.             the_cmd_arg.val.range.lc = cucol;
  1774.             if (*prompt == '\'')
  1775.               {
  1776.             the_cmd_arg.val.range.hr = curow;
  1777.             the_cmd_arg.val.range.hc = cucol;
  1778.               }
  1779.             else
  1780.               {
  1781.             the_cmd_arg.val.range.hr = mkrow;
  1782.             the_cmd_arg.val.range.hc = mkcol;
  1783.               }
  1784.             the_cmd_arg.is_set = 1;
  1785.             the_cmd_arg.do_prompt = 0;
  1786.             init_arg_text (&the_cmd_arg,
  1787.                    range_name (&the_cmd_arg.val.range));
  1788.             the_cmd_arg.style = &range_constant_style;
  1789.             goto next_arg;
  1790.           }
  1791.         case '[':
  1792.           {
  1793.             ++prompt;
  1794.             while (*prompt && (*prompt != ']'))
  1795.               if (*prompt != '\\')
  1796.             ++prompt;
  1797.               else
  1798.             {
  1799.               ++prompt;
  1800.               if (*prompt)
  1801.                 ++prompt;
  1802.             }
  1803.             if (*prompt == ']')
  1804.               ++prompt;
  1805.             
  1806.             if (get_argument (prompt, &menu_style))
  1807.               goto new_cycle;
  1808.             goto next_arg;
  1809.           }
  1810.         case '$':
  1811.           {
  1812.             /* Edit a cell's formula. */
  1813.             CELL * cp = find_cell (curow, cucol);
  1814.             ++prompt;
  1815.             if (((!cp || GET_LCK (cp) == LCK_DEF)
  1816.              && (default_lock == LCK_LCK))
  1817.             || (cp && GET_LCK (cp) == LCK_LCK))
  1818.               {
  1819.             io_error_msg ("Cell %s is locked",
  1820.                       cell_name (curow, cucol));
  1821.             pop_unfinished_command ();
  1822.             goto new_cycle;
  1823.               }
  1824.             the_cmd_frame->prev->_setrow = curow;
  1825.             the_cmd_frame->prev->_setcol = cucol;
  1826.             if (get_argument (prompt, &formula_style))
  1827.               {
  1828.             init_arg_text (&the_cmd_arg,
  1829.                        decomp (curow, cucol, cp));
  1830.             decomp_free ();
  1831.             goto new_cycle;
  1832.               }
  1833.             goto next_arg;
  1834.           }
  1835.           
  1836.         default:
  1837.           {
  1838.             io_error_msg ("Interaction-string error!!!");
  1839.             pop_unfinished_command ();
  1840.             goto new_cycle;
  1841.           }
  1842.         }
  1843.         }
  1844.     next_arg:
  1845.       ++cur_arg;
  1846.     }
  1847.  
  1848.       /* Make sure that all the args are really there. */
  1849.       for (cur_arg = 0; cur_arg < cmd_argc; ++cur_arg)
  1850.     if (the_cmd_arg.do_prompt && !the_cmd_arg.is_set)
  1851.       goto resume_getting_arguments;
  1852.  
  1853.       /* If this point is reached, call the interactive function,
  1854.        * destroy it's frame, and restart the cycle.
  1855.        */
  1856.       {
  1857.     int move_cursor = 0;
  1858.     struct command_frame * frame = the_cmd_frame;
  1859.     cmd_invoker stub = find_stub ();
  1860.     if (   frame->_curow != frame->prev->_curow
  1861.         || frame->_cucol != frame->prev->_cucol)
  1862.       {
  1863.         move_cursor = 1;
  1864.         io_hide_cell_cursor ();
  1865.       }
  1866.     remove_cmd_frame (frame);
  1867.     /* Add frame to the list of frames to be freed on error. */
  1868.     frame->next = running_frames;
  1869.     running_frames = frame;
  1870.     if (move_cursor)
  1871.       io_display_cell_cursor ();
  1872.     if (!stub)
  1873.       io_error_msg ("Don't know how to invoke %s!!!",
  1874.             frame->cmd->func_name);
  1875.     else
  1876.       stub (frame);
  1877.     running_frames = running_frames->next;
  1878.     frame->next = 0;
  1879.     free_cmd_frame (frame);
  1880.  
  1881.     /* If command_loop was called by execute_command, it should
  1882.      * return as soon as there is no more macro to evaluate.
  1883.       */
  1884.     if (!rmac && the_cmd_frame->input->prev_stream)
  1885.       {
  1886.         pop_input_stream ();
  1887.         return;
  1888.       }
  1889.     if (the_cmd_frame->cmd)
  1890.       goto resume_getting_arguments;
  1891.       }
  1892.     }
  1893. }
  1894.  
  1895. /* Exectute the command called in `string'.
  1896.  * If the string begins with a proper command name,
  1897.  * it is executed as if it were embedded in "{}" in 
  1898.  * a macro.  Otherwise, if the string can be interpreted 
  1899.  * as a range address, the macro at that address is executed.
  1900.  */
  1901.  
  1902. static struct line exec_cmd_line = {0, 0};
  1903.  
  1904. /* execute_command buils a macro expression of the from `{command args}'.
  1905.  * This function quotes the braces in ARGS so that the macro reader knows
  1906.  * they are literal rather than macro syntax.
  1907.  */
  1908. static void
  1909. quote_macro_args (args)
  1910.      char * args;
  1911. {
  1912.   while (*args)
  1913.     {
  1914.       switch (*args)
  1915.     {
  1916.     case '{':
  1917.       *args = 0x80 | 'b';
  1918.       break;
  1919.     case '}':
  1920.       *args = 0x80 | 'c';
  1921.       break;
  1922.     }
  1923.       ++args;
  1924.     }
  1925. }
  1926.  
  1927. #ifdef __STDC__
  1928. void 
  1929. execute_command (char *str)
  1930. #else
  1931. void 
  1932. execute_command (str)
  1933.      char *str;
  1934. #endif
  1935. {
  1936.   char *ptr = str;
  1937.   char * run;            /* The first string to execute. */
  1938.   /* The address of the macro to execute.  If the user typed a 
  1939.    * command name and not a range name, then this range will be
  1940.    * set to a one cell region.
  1941.    */
  1942.   struct rng rng;
  1943.   static struct line exec_buf = {0, 0};
  1944.   int count = 1;
  1945.  
  1946.   /* Chop off the first word. */
  1947.   while (isspace (*str))
  1948.     ++str;
  1949.   if (!*str || *str == '#')
  1950.     return;
  1951.   for (ptr = str; *ptr && !isspace (*ptr); ptr++);
  1952.   if (*ptr)
  1953.     {
  1954.       setn_line (&exec_buf, str, ptr - str + 1);
  1955.       exec_buf.buf[ptr - str] = 0;
  1956.       str  = exec_buf.buf;
  1957.       ++ptr;
  1958.     }
  1959.   else
  1960.     ptr = 0;
  1961.   
  1962.  
  1963.   /* First, look for a command name. */
  1964.   {
  1965.     int vector;
  1966.     struct cmd_func * cmd;
  1967.  
  1968.     if (!find_function (&vector, &cmd, str, strlen(str)))
  1969.       {
  1970.     if (ptr)
  1971.       {
  1972.         quote_macro_args (ptr);
  1973.         sprint_line (&exec_cmd_line, "{%s %s}", str, ptr);
  1974.       }
  1975.     else
  1976.       sprint_line (&exec_cmd_line, "{%s}", str);
  1977.     run = exec_cmd_line.buf;
  1978.     rng.lr = rng.hr = 1;
  1979.     rng.lc = rng.hc = 1;
  1980.     goto found_command;
  1981.       }
  1982.   }
  1983.   
  1984.   {
  1985.     /* Try for a range address. */
  1986.     CELL *cp;
  1987.     if (get_abs_rng (&str, &rng))
  1988.       {
  1989.     io_error_msg ("Unknown command %s", str);
  1990.     return;
  1991.       }
  1992.     if (ptr)
  1993.       {
  1994.     io_error_msg ("Macros can't take arguments");
  1995.     return;
  1996.       }
  1997.  
  1998.     cp = find_cell (rng.lr, rng.lc);
  1999.     if (!cp
  2000.     || GET_TYP (cp) != TYP_STR
  2001.     || cp->cell_str[0] == '\0')
  2002.       {
  2003.     io_error_msg ("No macro found at %s.", range_name (&rng));
  2004.     return;
  2005.       }
  2006.  
  2007.     run = cp->cell_str;
  2008.     /* Reset the keystate. */
  2009.     cur_keymap = the_cmd_frame->top_keymap;
  2010.     count = how_many;
  2011.     how_many = 1;        /* s.o.p when executing macros; */
  2012.     set_line (&raw_prefix, "");    /* see run_string_as_macro for more info*/
  2013.   }
  2014.  
  2015.  found_command:
  2016.   while (count-- > 0)
  2017.     {
  2018.       macro_only_input_stream (&rng, run, strlen (run), the_cmd_frame);
  2019.       command_loop (1);
  2020.     }
  2021. }
  2022.  
  2023.  
  2024.  
  2025.  
  2026. /* Read a character.  If we're in a macro, read from the macro. . . */
  2027. #ifdef __STDC__
  2028. int
  2029. get_chr (void)
  2030. #else
  2031. int
  2032. get_chr ()
  2033. #endif
  2034. {
  2035.   int ch;
  2036.  
  2037.   if (rmac)
  2038.     {
  2039.       ch = *(rmac->mac_exe++);
  2040.       switch (ch)
  2041.     {
  2042.     case '{':        /* What else can we do? */
  2043.     case '\0':
  2044.       --rmac->mac_exe;
  2045.       break;
  2046.  
  2047.     case (0x80 | 'a'):
  2048.       ch = 0;
  2049.       break;
  2050.  
  2051.     case (0x80 | 'b'):
  2052.       ch = '{';
  2053.       break;
  2054.     default:
  2055.       break;
  2056.     }
  2057.     }
  2058.   else
  2059.     ch = real_get_chr ();
  2060.   return ch;
  2061. }
  2062.  
  2063.  
  2064. /* This is an entirely magical function.  All of it's work is done 
  2065.  * by the argument prompting system.  All that remains to be done
  2066.  * when this is called is to push back a character the user may have
  2067.  * typed to cause the error message to go away.
  2068.  */
  2069.  
  2070. #ifdef __STDC__
  2071. void
  2072. display_error_msg (char * msg, int c)
  2073. #else
  2074. void
  2075. display_error_msg (msg, c)
  2076.      char * msg;
  2077.      int c;
  2078. #endif
  2079. {
  2080.   if (c > 0)
  2081.     pushed_back_char = c;
  2082. }
  2083.  
  2084. #ifdef __STDC__
  2085. void
  2086. pushback_keystroke (int c)
  2087. #else
  2088. void
  2089. pushback_keystroke (c)
  2090.      int c;
  2091. #endif
  2092. {
  2093.   if (c > 0)
  2094.     pushed_back_char = c;
  2095. }
  2096.  
  2097. #ifdef __STDC__
  2098. void
  2099. io_error_msg (char *str,...)
  2100. #else
  2101. void
  2102. io_error_msg (str, va_alist)
  2103.      char *str;
  2104.      va_dcl
  2105. #endif
  2106. {
  2107.   va_list foo;
  2108.   char buf[1000];
  2109.   char buf2[1000];
  2110.  
  2111.   /* This is made robust against errors that occur before
  2112.    * the io hooks have been initialized.
  2113.    */
  2114.   if (display_opened)
  2115.     io_bell ();
  2116.   
  2117.   var_start (foo, str);
  2118.   vsprintf (buf, str, foo);
  2119.   sprintf (buf2, "display-error-msg %s", buf);
  2120.   recover_from_error ();
  2121.   if (display_opened)
  2122.     execute_command (buf2);
  2123.   else
  2124.     fprintf (stderr, "oleo: %s\n", buf);
  2125.   longjmp (error_exception, 1);
  2126. }
  2127.  
  2128.  
  2129. #ifdef __STDC__
  2130. void
  2131. io_info_msg (char *str,...)
  2132. #else
  2133. void
  2134. io_info_msg (str, va_alist)
  2135.      char *str;
  2136.      va_dcl
  2137. #endif
  2138. {
  2139.   va_list foo;
  2140.   char buf[1000];
  2141.   char buf2[1000];
  2142.  
  2143.   var_start (foo, str);
  2144.   vsprintf (buf, str, foo);
  2145.   sprintf (buf2, "display-error-msg %s", buf);
  2146.   execute_command (buf2);
  2147. }
  2148.  
  2149.  
  2150.  
  2151.  
  2152. /* Expands a string that will be used to prompt for an argument.
  2153.  *    %n expands to the text of argument n (if defined -- ??? otherwise).
  2154.  *    %% expands to %
  2155.  *    %c expands to the name of the_cmd_frame->prev->_set{row,col}
  2156.  * If no expansion is needed, the argument is returned.  Otherwise,
  2157.  * malloced memory is returned.
  2158.  */
  2159.  
  2160. #ifdef __STDC__
  2161. char *
  2162. expand_prompt (char * str)
  2163. #else
  2164. char *
  2165. expand_prompt (str)
  2166.      char * str;
  2167. #endif
  2168. {
  2169.   struct line expanded;
  2170.   init_line (&expanded);
  2171.   if (!str || !index (str, '%'))
  2172.     return ck_savestr (str);
  2173.   {
  2174.     char * last_pos = str;
  2175.     char * src_pos;
  2176.  
  2177.     for (src_pos = index (str, '%'); src_pos; src_pos = index (src_pos, '%'))
  2178.       {
  2179.     catn_line (&expanded, last_pos, src_pos - last_pos);
  2180.     ++src_pos;
  2181.     switch (*src_pos)
  2182.       {
  2183.       case '%':
  2184.         catn_line (&expanded, src_pos, 1);
  2185.         ++src_pos;
  2186.         break;
  2187.       case 'c':
  2188.         {
  2189.           struct rng rng;
  2190.           char * str;
  2191.           rng.lr = rng.hr = the_cmd_frame->prev->_setrow;
  2192.           rng.lc = rng.hc = the_cmd_frame->prev->_setcol;
  2193.           str = range_name (&rng);
  2194.           catn_line (&expanded, str, strlen(str));
  2195.           ++src_pos;
  2196.           break;
  2197.         }
  2198.       case '.':
  2199.         {
  2200.           struct rng rng;
  2201.           char * str;
  2202.           rng.lr = rng.hr = the_cmd_frame->prev->_curow;
  2203.           rng.lc = rng.hc = the_cmd_frame->prev->_cucol;
  2204.           str = range_name (&rng);
  2205.           catn_line (&expanded, str, strlen(str));
  2206.           ++src_pos;
  2207.           break;
  2208.         }
  2209.       case '0': case '1': case '2': case '3': case '4':
  2210.       case '5': case '6': case '7': case '8': case '9':
  2211.         {
  2212.           int argn = *src_pos - '0';
  2213.           if (   (cmd_argc > argn)
  2214.           && the_cmd_frame->argv[argn].is_set
  2215.           && the_cmd_frame->argv[argn].text.buf)
  2216.         catn_line (&expanded, the_cmd_frame->argv[argn].text.buf,
  2217.                strlen (the_cmd_frame->argv[argn].text.buf));
  2218.           else
  2219.         catn_line (&expanded, "????", 4);
  2220.           ++src_pos;
  2221.           break;
  2222.         }
  2223.       default:
  2224.         catn_line (&expanded, "%", 1);
  2225.         break;
  2226.       }
  2227.     last_pos = src_pos;
  2228.       }
  2229.     catn_line (&expanded, last_pos, strlen(last_pos));
  2230.   }
  2231.   return expanded.buf;
  2232. }
  2233.  
  2234.  
  2235.  
  2236.  
  2237. /* Info commands */
  2238.  
  2239. #ifdef __STDC__
  2240. void
  2241. set_info (char * name)
  2242. #else
  2243. void
  2244. set_info (name)
  2245.      char * name;
  2246. #endif
  2247. {
  2248.   struct info_buffer * ib = (name ? find_or_make_info (name) : 0);
  2249.   
  2250.   if (the_cmd_frame->cmd && (the_cmd_arg.prompt_info != ib))
  2251.     {
  2252.       the_cmd_arg.info_line = 0;
  2253.       the_cmd_arg.prompt_info = ib;
  2254.     }
  2255.   if (!ib && name && *name)
  2256.     io_error_msg ("No information about %s.", name);
  2257. }
  2258.  
  2259.  
  2260. #ifdef __STDC__
  2261. void
  2262. page_info_backwards (int rep)
  2263. #else
  2264. void
  2265. page_info_backwards (rep)
  2266.      int rep;
  2267. #endif
  2268. {
  2269.   if (rep < 0)
  2270.     page_info (-rep);
  2271.   else if (the_cmd_frame->cmd && the_cmd_arg.prompt_info)
  2272.     {
  2273.       int vis_lines = (scr_lines - input_rows) / info_rows;
  2274.       int next = the_cmd_arg.info_line - vis_lines * rep;
  2275.       the_cmd_arg.info_line = ((next >= 0) ? next : 0);
  2276.     }
  2277.   else
  2278.     io_error_msg ("No info to page.");
  2279. }
  2280.  
  2281. #undef MAX
  2282. #define MAX(A,B) (((A) >= (B)) ? (A) : (B))
  2283.  
  2284. #ifdef __STDC__
  2285. void
  2286. page_info (int rep)
  2287. #else
  2288. void
  2289. page_info (rep)
  2290.      int rep;
  2291. #endif
  2292. {
  2293.   if (rep < 0)
  2294.     page_info_backwards (-rep);
  2295.   else if (the_cmd_frame->cmd && the_cmd_arg.prompt_info)
  2296.     {
  2297.       int vis_lines = (scr_lines - input_rows) / info_rows;
  2298.       int next = the_cmd_arg.info_line + vis_lines * rep;
  2299.       the_cmd_arg.info_line =
  2300.     ((next >= the_cmd_arg.prompt_info->len)
  2301.      ? MAX(0, (the_cmd_arg.prompt_info->len - vis_lines))
  2302.      : next);
  2303.     }
  2304.   else
  2305.     io_error_msg ("No info to page.");
  2306. }
  2307.  
  2308. #ifdef __STDC__
  2309. void
  2310. view_info (char * name, int ignore)
  2311. #else
  2312. void
  2313. view_info (name, ignore)
  2314.      char * name;
  2315.      int ignore;
  2316. #endif
  2317. {}
  2318.  
  2319.  
  2320.  
  2321. /* The C part of this function is uninteresting.  The interesting part
  2322.  * is in defun.h.
  2323.  */
  2324.  
  2325. #ifdef __STDC__
  2326. void
  2327. with_keymap (char * mapname)
  2328. #else
  2329. void
  2330. with_keymap (mapname)
  2331.      char * mapname;
  2332. #endif
  2333. {}
  2334.  
  2335. #ifdef __STDC__
  2336. void
  2337. one_cmd_with_keymap (char * mapname, struct key_sequence * keyseq)
  2338. #else
  2339. void
  2340. one_cmd_with_keymap (mapname, keyseq)
  2341.      char * mapname;
  2342.      struct key_sequence * keyseq;
  2343. #endif
  2344. {
  2345.   if (keyseq->cmd.vector < 0 && keyseq->cmd.code < 0)
  2346.     io_bell ();
  2347.   else if (keyseq->cmd.vector < 0)
  2348.     io_error_msg
  2349.       ("one-command-with-keymap: %s maps to a keymap (%s), not a command.",
  2350.        keyseq->keys->buf, map_names[keyseq->cmd.code]);
  2351.   else
  2352.     execute_command
  2353.       (the_funcs[keyseq->cmd.vector][keyseq->cmd.code].func_name);
  2354. }
  2355.